"""行存压缩相关公共函数"""
import re

from yat.test import Node
from yat.test import macro

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class ComRowCompress:
    def __init__(self, node_name='PrimaryDbUser'):
        self.log = Logger()
        self.Constant = Constant()
        self.pri_sh = CommonSH(node_name)
        self.pri_node = Node(node=node_name)

    def create_compare_table(self, compress_tb, nocompress_tb):
        """创建行存压缩表与非行存压缩表，用以对比数据
        :compress_tb: 行存压缩表表名
        :nocompress_tb: 非行存压缩表表名
        :return: 返回执行结果
        """
        str_val = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx"
        cloumns = f"(COLUMNONE INTEGER, " \
            f"COLUMNTWO CHAR(50) default '{str_val}'," \
            f"COLUMNTHREE VARCHAR(50) default '{str_val}', " \
            f"COLUMNFOUR INTEGER default 1," \
            f"COLUMNFIVE CHAR(50) default '{str_val}', " \
            f"COLUMNSIX VARCHAR(50) default '{str_val}', " \
            f"COLUMNSEVEN CHAR(50) default '{str_val}', " \
            f"COLUMNEIGHT CHAR(50) default '{str_val}', " \
            f"COLUMNNINE VARCHAR(50) default '{str_val}', " \
            f"COLUMNTEN VARCHAR(50) default '{str_val}', " \
            f"COLUMNELEVEN CHAR(50) default '{str_val}', " \
            f"COLUMNTWELVE CHAR(50) default '{str_val}', " \
            f"COLUMNTHIRTEEN VARCHAR(50) default '{str_val}', " \
            f"COLUMNFOURTEEN CHAR(50) default '{str_val}', " \
            f"COLUMNFIFTEEM VARCHAR(50) default '{str_val}')"
        create_sql = f"drop table if exists {compress_tb};" \
            f"drop table if exists {nocompress_tb};" \
            f"create table {compress_tb}{cloumns} with (compresstype=1);" \
            f"create table {nocompress_tb}{cloumns}"
        result = self.pri_sh.execut_db_sql(create_sql)
        return result

    def check_data_content(self, compress_tb, nocompress_tb):
        """对比行存压缩表与非行存压缩表的数据是否一致
        :compress_tb: 行存压缩表表名
        :nocompress_tb: 非行存压缩表表名
        :return: 返回checksum结果
        """
        query_sql = f"select checksum({compress_tb}::text) " \
                    f"from {compress_tb};" \
                    f"select checksum({nocompress_tb}::text) " \
                    f"from {nocompress_tb};"
        self.log.info(query_sql)
        result = self.pri_sh.execut_db_sql(query_sql)
        return result

    def check_compress_file(self, compress_tb):
        """校验行存压缩表_compress文件是否成功生成
        :compress_tb: 行存压缩表表名
        :return: 成功生成文件返回文件实际大小，反之返回-1
        """
        query_sql = f"select relfilenode from pg_class " \
                    f"where relname='{compress_tb}';"
        self.log.info(query_sql)
        result = self.pri_sh.execut_db_sql(query_sql)
        self.log.info(result)
        if result.splitlines()[-1].strip() != '(1 row)':
            return -1

        relfilenode = result.splitlines()[-2].strip()
        query_cmd = f"du `find {macro.DB_INSTANCE_PATH} " \
                    f"-name '{relfilenode}*compress'` " \
                    f"| grep compress | awk \'{{{{print $1}}}}\'"
        self.log.info(query_cmd)
        query_res = self.pri_node.sh(query_cmd).result().splitlines()
        self.log.info(query_res)
        compress_size = 0
        for i in query_res:
            compress_size += int(i)
        return int(compress_size * 1024)
